package com.situ.web.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/ajax")
public class AjaxServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("AjaxServlet.doGet");

        //模拟后台花2秒执行耗时操作
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String name = req.getParameter("name");
        System.out.println("name: " + name);
        // {"name":"李四","age":23}
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("{\"name\":\"李四\",\"age\":23}");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("AjaxServlet.doPost");


        String name = req.getParameter("name");
        System.out.println("name: " + name);
        // {"name":"李四","age":23}
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("{\"name\":\"李四1\",\"age\":24}");

        //doGet(req, resp);
    }
}
